PROJECTENGRAM

NAIVE BAYES

07_Naive_Bayes

Tags: #◇CSINTSY #◇Computational Thinking #◇Computer Science #◇Artificial Intelligence #◇Machine Learning #◇Intelligent Systems #◇Probability #◇Data Analysis


◇Overview

Naive Bayes is a probabilistic classifier based on Bayes' Theorem. It is called "Naive" because it makes a strong assumption that features are inde...

◇Details / Theory

7. Naive Bayes Reviewer

◇Overview

Naive Bayes is a probabilistic classifier based on Bayes' Theorem. It is called "Naive" because it makes a strong assumption that features are independent of each other given the class. Despite this simplification, it works surprisingly well, especially for text classification (e.g., Spam Filtering).

◇Bayes' Theorem

P(C∣X)=P(X∣C)×P(C)P(X)P(C|X) = \frac{P(X|C) \times P(C)}{P(X)}
P(C∣X)P(C|X): Posterior Probability. The probability of Class CC given features XX. (What we want to find). P(X∣C)P(X|C): Likelihood. The probability of features XX appearing given Class CC. P(C)P(C): Prior Probability. The initial probability of Class CC (before seeing data). P(X)P(X): Evidence. The probability of features XX appearing. (Usually ignored in comparison since it's constant for all classes).

◇The "Naive" Assumption

We assume features x1,x2,…,xnx_1, x_2, \dots, x_n are independent.
P(X∣C)=P(x1∣C)×P(x2∣C)×⋯×P(xn∣C)P(X|C) = P(x_1|C) \times P(x_2|C) \times \dots \times P(x_n|C)

◇Classification Rule

To classify a new instance XX, we calculate the score for each class and pick the highest one:
Class=argmaxCP(C)∏i=1nP(xi∣C)Class = \text{argmax}_C P(C) \prod_{i=1}^{n} P(x_i|C)

◇Example: Spam Filter

Classes: Spam, Ham (Not Spam). Message: "Free Money". Calculation:
Score(Spam)=P(Spam)×P("Free"∣Spam)×P("Money"∣Spam)Score(Spam) = P(Spam) \times P("Free"|Spam) \times P("Money"|Spam)
Score(Ham)=P(Ham)×P("Free"∣Ham)×P("Money"∣Ham)Score(Ham) = P(Ham) \times P("Free"|Ham) \times P("Money"|Ham) Compare scores. If Score(Spam)>Score(Ham)Score(Spam) > Score(Ham), classify as Spam.

◇Laplace Smoothing (Add-One Smoothing)

Problem: If a word (feature) never appears in the training data for a specific class, its probability P(word∣Class)P(word|Class) will be 0. Consequence: Since we multiply probabilities, a single 0 will make the entire score 0, regardless of other evidence. Solution: Add a small count (usually 1) to all frequency counts.
P(xi∣C)=count(xi,C)+1count(C)+VP(x_i|C) = \frac{count(x_i, C) + 1}{count(C) + V}
VV: Vocabulary size (total number of unique words).
This ensures no probability is ever exactly zero.

◇Solved Example: Spam Classification

Dataset:
Spam (3 messages): "Buy cheap meds", "Cheap money", "Buy money" Ham (2 messages): "Cheap meds", "Study hard"

Vocabulary: {Buy, Cheap, Meds, Money, Study, Hard} (Size V=6V=6)

Priors:
P(Spam)=3/5=0.6P(Spam) = 3/5 = 0.6 P(Ham)=2/5=0.4P(Ham) = 2/5 = 0.4

Likelihoods (with Laplace Smoothing +1):
Spam Word Count: 6 words total.
Count("Cheap" | Spam) = 2.
P("Cheap"∣Spam)=2+16+6=312=0.25P("Cheap" | Spam) = \frac{2 + 1}{6 + 6} = \frac{3}{12} = 0.25
Count("Money" | Spam) = 2.
P("Money"∣Spam)=2+16+6=312=0.25P("Money" | Spam) = \frac{2 + 1}{6 + 6} = \frac{3}{12} = 0.25
Ham Word Count: 4 words total.
Count("Cheap" | Ham) = 1.
P("Cheap"∣Ham)=1+14+6=210=0.2P("Cheap" | Ham) = \frac{1 + 1}{4 + 6} = \frac{2}{10} = 0.2
Count("Money" | Ham) = 0.
P("Money"∣Ham)=0+14+6=110=0.1P("Money" | Ham) = \frac{0 + 1}{4 + 6} = \frac{1}{10} = 0.1

Test Message: "Cheap Money"

Calculate Scores:

  1. Spam Score:
    P(Spam)×P("Cheap"∣Spam)×P("Money"∣Spam)P(Spam) \times P("Cheap"|Spam) \times P("Money"|Spam)
    0.6×0.25×0.25=0.03750.6 \times 0.25 \times 0.25 = 0.0375

  2. Ham Score:
    P(Ham)×P("Cheap"∣Ham)×P("Money"∣Ham)P(Ham) \times P("Cheap"|Ham) \times P("Money"|Ham)
    0.4×0.2×0.1=0.0080.4 \times 0.2 \times 0.1 = 0.008

Conclusion:
Since 0.0375>0.0080.0375 > 0.008, the message is classified as Spam.


◇Common Pitfalls

Forgetting Laplace smoothing when a feature value has zero occurrences in a class — without it, the entire probability becomes 0. Violating the "naive" independence assumption without awareness — Naive Bayes assumes features are conditionally independent given the class. Comparing raw products instead of normalizing — final classification compares P(C)∏P(xi∣C)P(C) \prod P(x_i|C) across all classes.

◇Summary

TL;DR — Naive Bayes classifies by computing P(C∣X)∝P(C)∏P(xi∣C)P(C|X) \propto P(C) \prod P(x_i|C) for each class and picking the maximum. It assumes feature independence and uses Laplace smoothing to handle zero-count features.

Related: Source / Reference:


Made by 2ru17

Please contact me through discord or my the university email for any problems regarding the reviewer. Thanks!

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

Why is the Naive Bayes classifier called "Naive"?

Q2

In Bayes' Theorem, what does the term P(C∣X)P(C|X) represent?

Q3

Which term in Bayes' Theorem describes the probability of observing the features given a specific class, P(X∣C)P(X|C)?

Q4

When computing probabilities for different classes, why is the Evidence term P(X)P(X) often ignored?

Q5

How does Naive Bayes mathematically represent its independence assumption?

Q6

How does a Naive Bayes classifier finally determine the predicted class for a new instance?

Q7

What problem arises if a word in a test instance never appeared in the training data for a specific class?

Q8

What technique is used to solve the zero-probability problem in Naive Bayes?

Q9

In the context of Laplace smoothing, what does the variable VV represent in the formula?

Q10

If an email classification dataset has 10 Spam messages and 40 Ham messages, what is the Prior Probability of Spam, P(Spam)P(Spam)?